<?php

namespace app\admin\model\advertiser;

use aliyun\AliyunOss;
use app\BaseModel;

/**
 * 广告主推广物模型
 */
class AdvertiserMatter extends BaseModel
{

    /**
     * 获取推广物列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $data = self::where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        $AliyunOss = new AliyunOss();
        //遍历数组
        foreach ($data['data'] as &$v) {
            $v['avatar_url'] = $AliyunOss->getObjectUrl($v['avatar']);
            $v['qrcode_url'] = $AliyunOss->getObjectUrl($v['qrcode']);
        }
        return $data;
    }

    /**
     * 获取全部推广物列表
     * @param array $where
     * @param string $orderby
     * @return array
     */
    public static function getListAll(array $where, string $orderby = ''): array
    {
        $data = self::field('id,ad_user_id,type,name,avatar,qrcode')
            ->where($where)
            ->order($orderby)
            ->select()
            ->toArray();
        $AliyunOss = new AliyunOss();
        //遍历数组
        foreach ($data as &$v) {
            $v['avatar_url'] = $AliyunOss->getObjectUrl($v['avatar']);
            $v['qrcode_url'] = $AliyunOss->getObjectUrl($v['qrcode']);
        }
        return $data;
    }

    public static function getMatterInfo(int $id): array
    {
        //查询数据库
        $field = 'id,appid,ad_user_id,type,name,desc,avatar,qrcode,auth,auth_info,auth_time,closed';
        $data = self::field($field)
            ->where(['id' => $id])
            ->cache(true, 60)
            ->find();
        return $data ? $data->toArray() : [];
    }

}